import router from '@/router'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'
import Router from 'vue-router'
import role from './router/modules/role'

/**
 *前置守卫
 *
*/
let dataFetched = false; // 添加一个标记来表示数据是否已经被获取

const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  nprogress.start()
  if (store.getters.token) {
    // 存在token
    if (to.path === '/login') {
      // 跳转到主页
      next('/') // 中转到主页
      // next（地址）并没有执行后置守卫
      nprogress.done()
    } else {
      //判读是否获取过资料‘
      if(!store.getters.userId){
        console.log(store.getters.userInfo)
        try {
          const userInfo = await store.dispatch('user/getUserInfo');
        }catch (e){
          next()
        }
        next()
        console.log("2")
        await store.dispatch('user/getEmployInfo');
      }
      next() // 放过
    }
  } else {
    // 没有token
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login') // 中转到登录页
      nprogress.done()
    }
  }
})

/** *
 * 后置守卫
 * **/
router.afterEach(() => {
  nprogress.done()
})
